package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.domian.Category;
import com.itheima.reggie.domian.Dish;
import com.itheima.reggie.domian.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishServiceImpl;
    @Autowired
    private SetmealService setmealServiceImpl;

    @Override
    public boolean remove(long id) {
        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<> ();
        dishLqw.eq ( Dish::getCategoryId, id );
        int dish = dishServiceImpl.count ( dishLqw );
        if (dish >0) {
            throw new CustomException ( "当前分类下关联了菜品，不能删除" );
        }

        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<> ();
        setmealLqw.eq ( Setmeal::getCategoryId,id );
        int setmeal = setmealServiceImpl.count ( setmealLqw );
        if (setmeal >0) {
            throw new CustomException ( "当前分类下关联了套餐，不能删除" );
        }

        boolean result = super.removeById ( id );
        return result ? true : false;

    }
}
